#include "stdafx.h"
#include "boucle.h"
#include "econ.h"



Boucle::Boucle()
{
	code = CODE_WHILE;
}

Boucle::~Boucle() 
{
	delete this->e;
}

Retour Boucle::evaluer(map<string, Var_T> *loc, map<string, Var_T> *glob)
{
	Retour ret;

	if(e->evaluer(loc, glob).getBool()) {
		ret.code = RET_LOOPING;
	} else {
		ret.code = RET_NOT_LOOPING;
	}

	return ret;
}

Instruction* boucler(list<token> src)
{
	Boucle *loop = new Boucle();

	src.pop_front();

	if (src.front().tok != "(")
		die("Error: Expected opening parenthesis after 'while' keyword.");

	if (src.back().tok != ")")
		die("Error: Expected closing parenthesis after 'while' statement.");

	src.pop_front();
	src.pop_back();

	if (validerEcon(src))
	{
		Econ *e = new Econ();
		e->construire(e->convertir(e->separer(src)));
		loop->e = e;
	}
	else
	{
		die("Error: Invalid conditional expression in loop.");
	}

	return loop;
}
